@echo off

REM 设置变量
set JAVA_HOME=C:\Program Files\Java\jdk-21
set M2_REPO=%USERPROFILE%\.m2\repository
set SRC_DIR=src\main\java
set RES_DIR=src\main\resources
set TARGET_DIR=target\classes
set MAIN_CLASS=com.example.deepseek.backend.DeepSeekBackendApplication

REM 创建目标目录
if not exist "%TARGET_DIR%" mkdir "%TARGET_DIR%"

REM 复制资源文件
xcopy /s /y "%RES_DIR%\*" "%TARGET_DIR%" >nul

REM 构建类路径
set CLASSPATH=
set CLASSPATH=%CLASSPATH%;%TARGET_DIR%
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\springframework\boot\spring-boot-starter-web\2.7.18\spring-boot-starter-web-2.7.18.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\springframework\boot\spring-boot-starter-log4j2\2.7.18\spring-boot-starter-log4j2-2.7.18.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\springframework\boot\spring-boot\2.7.18\spring-boot-2.7.18.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\com\fasterxml\jackson\core\jackson-databind\2.15.3\jackson-databind-2.15.3.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\commons-io\commons-io\2.13.0\commons-io-2.13.0.jar
set CLASSPATH=%CLASSPATH%;%M2_REPO%\org\apache\pdfbox\pdfbox\2.0.29\pdfbox-2.0.29.jar

REM 编译Java源码
"%JAVA_HOME%\bin\javac.exe" -cp "%CLASSPATH%" -d "%TARGET_DIR%" -sourcepath "%SRC_DIR%" "%SRC_DIR%\com\example\deepseek\backend\%MAIN_CLASS%.java" "%SRC_DIR%\com\example\deepseek\backend\**\*.java"

REM 检查编译是否成功
if %ERRORLEVEL% neq 0 (
    echo 编译失败！
    pause
    exit /b 1
)

REM 运行应用程序
"%JAVA_HOME%\bin\java.exe" -cp "%CLASSPATH%" %MAIN_CLASS%

pause